
package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.MessageApi;
import com.tanhua.dubbo.server.pojo.MessagesPojo;
import com.tanhua.dubbo.server.pojo.Totals;
import com.tanhua.server.mapper.UserMapper;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessagesService {

    @Reference(version = "1.0.0")
    private MessageApi messageApi;


    //消息通过
    public Boolean messagesPass(String[] ids) {

        try {
            for (String id : ids) {
                boolean b = messageApi.messagesPass(null,id, "5");
                if (!b)
                    return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //拒绝
    public Boolean messagesReject(String[] ids) {
        try {
            for (String id : ids) {
                boolean b = messageApi.messagesReject(null,id, "4");
                if (!b)
                    return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //撤销
    public Boolean messagesRevocation(String[] ids) {
        try {
            //此处为通过id查找动态详情
            for (String id : ids) {
                MessagesPojo messagesPojo = messageApi.queryMessageById(id);
                boolean b = messageApi.messageRevocation(messagesPojo.getUserId(),id, "1");

            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //动态翻页
    public PageResult queryMessages(Integer page, Integer pageSize, String sortProp, String sortOrder,
                                    String id, String sd, String ed, String userId, String state) {

        PageResult pageResult=new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        List<MessagesPojo> message = getMessage(userId,page,pageSize,sd, ed, id, pageResult, sortProp, sortOrder, state);
        List<Totals> totalsList=messageApi.queryCountCode();
        pageResult.setTotals(totalsList);
        pageResult.setItems(message);
        if ("all".equals(state)){
            long count =messageApi.queryCount();
            int counta= (int) count;
            pageResult.setCounts(counta);
        }else{
            for (Totals totals : totalsList) {
                if (totals.getCode().equals(state)){
                    pageResult.setCounts(totals.getValue());
                }
            }
        }
        return pageResult;
    }

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMessageService userInfoService;

    //填充数据
    private List<MessagesPojo> getMessage(String userId,Integer page,Integer pageSize,String sd,String ed,String id,PageResult pageResult,String sortProp,String sortOrder,String state) {

        List<MessagesPojo> messages = messageApi.queryByQuery(userId,page,pageSize,id, sd, ed,sortProp, sortOrder,state);

        List<MessagesPojo> messagesPojoList=new ArrayList<>();
        for (MessagesPojo message : messages) {
            //注入数据//作者昵称,作者头像,点赞数,评论数
            //            //根据userId查询用户nickName,logo
            //userInfo通过user_id字段返回来访用户的具体信息
            UserInfo userInfo = userInfoService.queryUserInfoByUserId(message.getUserId());
            message.setNickname(userInfo.getNickName());
            message.setUserLogo(userInfo.getLogo());
            //            //查询点赞数
            Long commentCount = messageApi.queryCommentCount(message.getId(), 2);
            int comment = commentCount.intValue();
            Long likeCount = messageApi.queryCommentCount(message.getId(), 1);
            int like = likeCount.intValue();
//            message.setCommentCount((messageApi.queryCommentCount(message.getId(),2)));
            message.setLikeCount(like);
            message.setCommentCount(comment);
            messagesPojoList.add(message);
        }
        System.out.println(messagesPojoList);
        return messagesPojoList;
    }


    //置顶
    public Boolean topMessage(String id) {
        Boolean aBoolean = messageApi.topMessage(id);
        return aBoolean;
    }

    //取消置顶
    public Boolean unTopMessage(String id) {
        Boolean aBoolean = messageApi.unTopMessage(id);
        return aBoolean;
    }
}

